package se.vgregion.excel.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;

import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.ResourceResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import se.vgregion.excel.types.CubeExcelFile;
import se.vgregion.excel.repository.CubeExcelService;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;

/**
 * Controller class for managing the Vega Ecell view.
 * 
 * @author claes.lundahl
 */
@Controller
@Component
@RequestMapping(value = "VIEW")
public class ExcelController {

    @Autowired
    private CubeExcelService vegaService;

    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelController.class);

    static final String START_FORM_JSP = "startForm";
    static final String VIEW_JSP = "view";

    /**
     * Renders the view, fetches data to display.
     * 
     * @param request
     *            incoming information from the client.
     * @param model
     *            place to put information ment for the page to render.
     * @return what view to use.
     */
    @RenderMapping
    public String show(RenderRequest request, Model model) {
        PortletSession portletSession = request.getPortletSession();
        String personId = (String) portletSession.getAttribute("personId");
        Collection<CubeExcelFile> files = vegaService.fetch();
        model.addAttribute("files", files);
        return START_FORM_JSP;
    }

    /**
     * Outputs a excel document with the user name filled in.
     * 
     * @param documentId
     * @param response
     * @param os
     * @param pr
     * @throws IOException
     * @throws PortalException
     * @throws SystemException
     */
    @ResourceMapping("resourceUrl")
    public void showDocumentAsResource(@RequestParam Long documentId, ResourceResponse response, OutputStream os,
            PortletRequest pr) throws IOException, PortalException, SystemException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        User user = fetchUser(pr);
        vegaService.findStreamFromExcelCubeFile(documentId, user.getScreenName(), os);
        os.flush();
    }

    public CubeExcelService getVegaService() {
        return vegaService;
    }

    public void setVegaService(CubeExcelService vegaService) {
        this.vegaService = vegaService;
    }

    protected User fetchUser(PortletRequest request) throws PortalException, SystemException {
        return PortalUtil.getUser(request);
    }

}
